package com.example.myapplication;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

import com.example.myapplication.databinding.FragmentVideoBinding;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class VideoFragment extends BaseFragment {
    private static final String NUMBER_TAG = "number";
    private static final String CHANNEL_TAG = "channel";
    private String devNumber;//imei
    private int channel;//imei
    private FragmentVideoBinding videoBinding;
    private String baseUrl = "wss://wss.iopadas.com:21078/tsinglive/";
    private String randomString = "TkE0zZZ6vS6yIvqo";
    private String wsUrl = "";
    private boolean play = false;

    public static VideoFragment newInstance(String number, int channel) {
        VideoFragment fragment = new VideoFragment();
        Bundle args = new Bundle();
        args.putString(NUMBER_TAG, number);
        args.putInt(CHANNEL_TAG, channel);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("fragment", "onCreate:" + channel);
        if (getArguments() != null) {
            devNumber = getArguments().getString(NUMBER_TAG);
            channel = getArguments().getInt(CHANNEL_TAG);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.e("fragment", "onPause:" + channel);
        stopVideo();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.e("fragment", "onCreateView:" + channel);
        videoBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_video, container, false);
        return videoBinding.getRoot();
    }

    MediaPlayHelper helper;
    Animation alphaAnimation;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Log.e("fragment", "onViewCreated:" + channel);
        videoBinding.tvCameraName.setText("通道" + channel);
        String md5 = MD5Util.md5(getTimesmorning() + randomString);
//        wsUrl = baseUrl + devNumber + "_" + channel + "_realtime?token=" + md5;
        wsUrl = baseUrl + devNumber + "_" + channel + "_realtime?token=" + "25fd661d308c8f41dbc923063d8b2056";
        videoBinding.tvWs.setText("视频播放地址:" + "\n" + wsUrl);
        helper = MediaPlayHelper.getInstance(videoBinding.surfaceView, getActivity(), wsUrl);
        videoBinding.surfaceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (play) {
                    stopVideo();
                } else {
                    startVideo();
                }
            }
        });
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            Log.e("fragment", "setUserVisibleHint:可见" + channel);
            if (helper != null) {
                startVideo();
            }
        } else {
            Log.e("fragment", "setUserVisibleHint:隐藏" + channel);
            if (helper != null) {
                stopVideo();
            }
        }
    }


    public void startVideo() {
        helper.startConnect();
        play = true;
        videoBinding.ivPlay.setBackground(getResources().getDrawable(R.mipmap.dev_pause));
        Log.e("fragment", "loadData:听说只会走一次" + channel);
        AlphaAnimation hideAnim = new AlphaAnimation(1, 0);
        hideAnim.setDuration(1000);
        videoBinding.ivPlay.startAnimation(hideAnim);
        hideAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                videoBinding.ivPlay.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    public void stopVideo() {
        helper.closeConnect();
        play = false;
        videoBinding.ivPlay.setVisibility(View.VISIBLE);
        videoBinding.ivPlay.setBackground(getResources().getDrawable(R.mipmap.dev_play));
//        Animation alphaAnimation = AnimationUtils.loadAnimation(getActivity(),
//                R.anim.image_stop);
//        videoBinding.ivPlay.setAnimation(alphaAnimation);
//        ScaleAnimation showAnim = new ScaleAnimation(0.8f, 0.5f, 0.8f, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
//                Animation.RELATIVE_TO_SELF, 0.5f);
//        showAnim.setDuration(500);
//        videoBinding.ivPlay.startAnimation(showAnim);
    }

    public String getTimesmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        Date beginOfDate = cal.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(beginOfDate);
    }
}
